package com.mstar.qs.common.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.morningstar.qs.api.basic.common.config.GlobalConfig;
import com.mstar.qs.common.Entry;

public class JDBCManager {
	
	public static final String database_type_mysql="mysql";
	public static final String database_type_mssql="mssql";

	private static Logger log = Logger.getLogger(JDBCManager.class.getPackage()
			.getName());

	/**
	 * 
	 * @param di
	 * @param type
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	@Deprecated
	public static Connection obtainConnection(DatabaseConnectiveInfo di,
			DBProvider type) throws ClassNotFoundException, SQLException {
		di.setDbType(type);
		return obtainConnection(di);
	}
	
	public static Connection obtainConnection(DatabaseConnectiveInfo di) throws ClassNotFoundException, SQLException {
		Connection conn = null;
		String url=null;
		switch (di.getDbType()) {
		case MYSQL:
			Class.forName("com.mysql.jdbc.Driver");
			url = "jdbc:mysql://" + di.getHost() + ":" + di.getPort() + "/"
					+ di.getSchema();
			break;
		case MSSQL:
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//			url="jdbc:microsoft:sqlserver://DBServerIP:1433;databaseName=master";
			url="jdbc:sqlserver://"+di.getHost() + ":" + di.getPort()+";databaseName="+di.getSchema();
			break;
		default:
			throw new IllegalArgumentException("unsupported database: "+di.getDbType());
		}


		// JSONObject o=new JSONObject(di);
		log.debug("DBInfo:URL: " + url);
		conn = DriverManager.getConnection(url, di.getUser(), di.getPwd());
		return conn;

	}
	
	public static Connection obtainConnection(Entry en) throws ClassNotFoundException, SQLException {
		DatabaseConnectiveInfo di=obtainDbInfo(en);
		return obtainConnection(di);
	}
	
	

	public static DatabaseConnectiveInfo obtainDbInfo(String elName) {
		DatabaseConnectiveInfo dbInfo = new DatabaseConnectiveInfo();
		dbInfo.setHost(GlobalConfig.getProperty(elName, GlobalConfig.HOST));
		dbInfo.setPort(Integer.valueOf(GlobalConfig.getProperty(elName,
				GlobalConfig.PORT)));
		dbInfo.setPwd(GlobalConfig.getProperty(elName, GlobalConfig.PWD));
		dbInfo.setSchema(GlobalConfig.getProperty(elName, GlobalConfig.SCHEMA));
		dbInfo.setUser(GlobalConfig.getProperty(elName, GlobalConfig.USER));
		String type=GlobalConfig.getProperty(elName, "type");
		if(type!=null)
		dbInfo.setDbType(type);
		return dbInfo;
	}
	
	public static DatabaseConnectiveInfo obtainDbInfo(Entry en) {
		DatabaseConnectiveInfo dbInfo = new DatabaseConnectiveInfo();
		dbInfo.setHost(en.attribute(GlobalConfig.HOST));
		dbInfo.setPort(Integer.valueOf(en.attribute(GlobalConfig.PORT)));
		dbInfo.setPwd(en.attribute(GlobalConfig.PWD));
		dbInfo.setSchema(en.attribute(GlobalConfig.SCHEMA));
		dbInfo.setUser(en.attribute(GlobalConfig.USER));
		String type=en.attribute("type");
		if(type!=null)
		dbInfo.setDbType(type);
		return dbInfo;
	}
	

	public static Connection obtainQSDBConnection()
			throws ClassNotFoundException, SQLException {
		String host = GlobalConfig.getProperty(GlobalConfig.QS_DATABASE,
				GlobalConfig.HOST);
		int port = Integer.valueOf(GlobalConfig.getProperty(
				GlobalConfig.QS_DATABASE, GlobalConfig.PORT));
		String schema = GlobalConfig.getProperty(GlobalConfig.QS_DATABASE,
				GlobalConfig.SCHEMA);
		String user = GlobalConfig.getProperty(GlobalConfig.QS_DATABASE,
				GlobalConfig.USER);
		String password = GlobalConfig.getProperty(GlobalConfig.QS_DATABASE,
				GlobalConfig.PWD);
		String type = GlobalConfig.getProperty(GlobalConfig.QS_DATABASE,
				"type");
		DatabaseConnectiveInfo di = new DatabaseConnectiveInfo();
		di.setHost(host);
		di.setPort(port);
		di.setPwd(password);
		di.setSchema(schema);
		di.setUser(user);
		if(type!=null) di.setDbType(type);
		Connection connect = obtainConnection(di, DBProvider.MYSQL);
		return connect;
	}

	public static enum DBProvider {
		MYSQL, MSSQL;
	}

}
